package engine.operators;

import java.util.Collections;
import java.util.List;

import engine.ObjectiveFunction;
import engine.Operator;
import engine.Population;
import engine.individuals.util.IndividualsComparator;

public class WorstHalfCutoff<T> implements Operator<T> {

	private IndividualsComparator<T> comparator;

	public WorstHalfCutoff(ObjectiveFunction<T> objFun) {
		this.comparator = new IndividualsComparator<T>(objFun);
	}

	@Override
	public Population<T> apply(Population<T> population) {

		List<T> individuals = population.getIndividuals();
		Collections.sort(individuals, comparator);
		int size = individuals.size();
		Population<T> result = new Population<T>(
				individuals.subList(size / 2, size));
		return result;
	}

}
